(* la fonction principale *)
let src = ref "map.bmp"
let dst = ref "map_final.bmp"
let tmp = ref ".tmp_map.bmp"
let pas = ref 20
let filtre = ref 0
let el = ref 0
let l = ref []
let triangulation = ref 3
let quadrillage_ou_point_obj = ref 1
let quad = ref []
let liste = ref []
let liste_hrgb = ref [(0,0,0,0)]

let rec wait_key () =
  let e = Sdlevent.wait_event () in
    match e with
    Sdlevent.KEYDOWN _ -> ()
      | _ -> wait_key ()

(* init de SDL *)
let sdl_init () =
  begin
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end

(* Dimensions d'une image *)
let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w,
   (Sdlvideo.surface_info img).Sdlvideo.h)

let rec insert (r, g, b) l = match l with (*insere en ordre croissant*)
  |hd::tl when hd < (r, g, b) -> hd::(insert (r, g, b) tl)
  |hd::_ as l when  (r, g, b) = hd -> l
  |l -> (r, g, b)::l


let rech_ins () =
  begin
    (* Initialisation de SDL *)
    sdl_init ();
    (* Chargement d'une image *)
    let img = Sdlloader.load_image !src in
    for x = 0 to ((Sdlvideo.surface_info img).Sdlvideo.w - 1) do
      for y = 0 to ((Sdlvideo.surface_info img).Sdlvideo.h - 1) do
        liste := (insert ((Sdlvideo.get_pixel_color img x y)) (!liste))
      done
    done;
  end

let main () =
  begin
    (*quadrillage_ou_point_obj : BoutonTRAITER afficher juste le quad = 0; Bouton3D faire le .obj = 1*)
    if !quadrillage_ou_point_obj = 0 then (* TRAITER *)
      begin
          ignore(Detourn.main !src !tmp !filtre !el !l ());
        if !triangulation <> 3 then
	  quad := Quad.main !tmp !dst !pas !quadrillage_ou_point_obj ()
        else
          Quadtree.main !src !tmp !dst !liste_hrgb !pas !quadrillage_ou_point_obj ()
      end
    else (* 3D *)
      begin
	(*triangulation : Normal = 1; Delauney = 2; Quadtree = 3*)
        if !triangulation <> 3 then
          begin
	    Quad.main !tmp !dst !pas !quadrillage_ou_point_obj ();
	    quad := Quad.main !tmp !dst !pas !quadrillage_ou_point_obj ();
            if !triangulation = 2 then
              begin
                DelaunS.main !src !quad !liste_hrgb ();
              end
            else
              begin
                Makeobj.main !src !pas !liste_hrgb ();
              end
          end
        else
          begin
            Quadtree.main !src !tmp !dst !liste_hrgb !pas !quadrillage_ou_point_obj ();
          end
      end
  end
